/****************************************************************************/
/*  File:       ResourceHelper.java                                         */
/*  Author:     F. Georges - fgeorges.org                                   */
/*  Date:       2009-09-07                                                  */
/*  Tags:                                                                   */
/*      Copyright (c) 2009 Florent Georges (see end of file.)               */
/* ------------------------------------------------------------------------ */


package org.expath.pkg.exist;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * TODO<doc>: ...
 *
 * @author Florent Georges
 * @date   2009-09-07
 */
public class ResourceHelper
{
    public static void copyResource(String name, File target)
            throws PackageException
    {
        try {
            ClassLoader loader  = ResourceHelper.class.getClassLoader();
            InputStream  in     = loader.getResourceAsStream(name);
            OutputStream out    = new FileOutputStream(target);
            byte[]       buffer = new byte[1024];
            int len;
            while( (len = in.read(buffer)) >= 0 ) {
                out.write(buffer, 0, len);
            }
            in.close();
            out.close();
        }
        catch ( FileNotFoundException ex ) {
            throw new PackageException("file not found", ex);
        }
        catch ( IOException ex ) {
            throw new PackageException("I/O error", ex);
        }
    }
}


/* ------------------------------------------------------------------------ */
/*  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS COMMENT.               */
/*                                                                          */
/*  The contents of this file are subject to the Mozilla Public License     */
/*  Version 1.0 (the "License"); you may not use this file except in        */
/*  compliance with the License. You may obtain a copy of the License at    */
/*  http://www.mozilla.org/MPL/.                                            */
/*                                                                          */
/*  Software distributed under the License is distributed on an "AS IS"     */
/*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See    */
/*  the License for the specific language governing rights and limitations  */
/*  under the License.                                                      */
/*                                                                          */
/*  The Original Code is: all this file.                                    */
/*                                                                          */
/*  The Initial Developer of the Original Code is Florent Georges.          */
/*                                                                          */
/*  Contributor(s): none.                                                   */
/* ------------------------------------------------------------------------ */
